package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description 《java编程思想》第22章 示例16：
 * <p>
 * 复选框
 **/
public class CheckBoxes extends JFrame {
    private JTextArea t = new JTextArea(6, 15);

    private JCheckBox cb1 = new JCheckBox("Check Box 1");
    private JCheckBox cb2 = new JCheckBox("Check Box 2");
    private JCheckBox cb3 = new JCheckBox("Check Box 3");

    private JButton clearText = new JButton("Clear Text");

    public CheckBoxes() {
        cb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                trace("1", cb1);
            }
        });

        cb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                trace("2", cb2);
            }
        });

        cb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                trace("3", cb3);
            }
        });

        setLayout(new FlowLayout());

        add(new JScrollPane(t));
        add(cb1);
        add(cb2);
        add(cb3);

        add(clearText);
        clearText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                t.setText("");
            }
        });
    }

    private void trace(String b, JCheckBox cb) {
        if (cb.isSelected()) {
            t.append("Box " + b + " Set\n");
        } else {
            t.append("Box " + b + " Cleared\n");
        }

    }

    public static void main(String[] args) {
        SwingConsole.run(new CheckBoxes(), 200, 300);
    }
}
